package com.lfy.fyadmin.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.lfy.fyadmin.common.R;
import com.lfy.fyadmin.dto.MenuTreeNode;
import com.lfy.fyadmin.dto.UserMenuResponse;
import com.lfy.fyadmin.service.MenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单控制器
 */
@RestController
@RequestMapping("/api/menu")
@Tag(name = "菜单管理", description = "菜单相关接口")
public class MenuController {
    
    private final MenuService menuService;
    
    public MenuController(MenuService menuService) {
        this.menuService = menuService;
    }
    
    @GetMapping("/current")
    @SaCheckLogin
    @Operation(summary = "获取当前用户菜单", description = "根据当前登录用户的权限获取菜单树")
    public R<UserMenuResponse> getCurrentUserMenu() {
        UserMenuResponse menuResponse = menuService.getCurrentUserMenu();
        return R.ok(menuResponse);
    }
    
    @GetMapping("/user/{userId}")
    @SaCheckLogin
    @Operation(summary = "根据用户ID获取菜单", description = "根据指定用户ID获取该用户的菜单树")
    public R<UserMenuResponse> getUserMenu(
            @Parameter(description = "用户ID", required = true)
            @PathVariable Long userId) {
        UserMenuResponse menuResponse = menuService.getUserMenuByUserId(userId);
        return R.ok(menuResponse);
    }
    
    @GetMapping("/all")
    @SaCheckLogin
    @Operation(summary = "获取所有菜单树", description = "获取系统中所有的菜单树结构（管理员使用）")
    public R<List<MenuTreeNode>> getAllMenuTree() {
        List<MenuTreeNode> menuTree = menuService.getAllMenuTree();
        return R.ok(menuTree);
    }
    
    @PostMapping("/tree")
    @SaCheckLogin
    @Operation(summary = "根据权限ID构建菜单树", description = "根据提供的权限ID列表构建菜单树")
    public R<List<MenuTreeNode>> buildMenuTree(
            @Parameter(description = "权限ID列表", required = true)
            @RequestBody List<Long> permissionIds) {
        List<MenuTreeNode> menuTree = menuService.buildMenuTree(permissionIds);
        return R.ok(menuTree);
    }
}